added SSCLI 1.0
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / CSASPNETIPtoLocation / App_Code / IPConvert.cs
blobec0c629b0c90032da1f9be8aa293c819f31a8c25
1 /**************************** Module Header ********************************\
2 * Module Name: IPConvert.cs
3 * Project: CSASPNETIPtoLocation
4 * Copyright (c) Microsoft Corporation
6 * This project illustrates how to get the geographical location from a db file.
7 * You need install Sqlserver Express for run the web applicaiton. The code-sample
8 * only support Internet Protocol version 4.
9 *
10 * This class use to calculate the IP number from IP address.
12 * This source is subject to the Microsoft Public License.
13 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
14 * All other rights reserved.
16 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
17 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
19 \*****************************************************************************/
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
25 using System.Web;
26 using System.Text;
28 namespace CSASPNETIPtoLocation
30 public class IPConvert
32 public static string ConvertToIPRange(string ipAddress)
34 try
36 string[] ipArray = ipAddress.Split('.');
37 int number = ipArray.Length;
38 double ipRange = 0;
39 if (number != 4)
41 return "error ipAddress";
43 for (int i = 0; i < 4; i++)
45 int numPosition = int.Parse(ipArray[3 - i].ToString());
46 if (i == 4)
48 ipRange += numPosition;
50 else
52 ipRange += ((numPosition % 256) * (Math.Pow(256, (i))));
55 return ipRange.ToString();
57 catch (Exception)
59 return "error";